package com.sunseen.intelligencebus

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import android.text.InputType
import android.view.View
import android.widget.ProgressBar
import android.widget.Toast
import com.avos.avoscloud.AVException
import com.avos.avoscloud.AVUser
import com.avos.avoscloud.LogInCallback
import com.sunseen.intelligencebus.databinding.FragmentLoginBinding
import com.sunseen.intelligencebus.base.AbsBindingFragment
import com.sunseen.intelligencebus.common.Preference

class LoginFragment : AbsBindingFragment() {

    companion object {
        fun newInstance(): LoginFragment {
            return LoginFragment()
        }
    }

    private var isLogin: Boolean by Preference(Preference.LOGIN_STATE, false)

    lateinit var loginProgress: ProgressBar

    override fun setLayout(): Int = R.layout.fragment_login

    override fun onBindView(viewDataBinding: ViewDataBinding,bundle: Bundle?) {
        val binding = viewDataBinding as FragmentLoginBinding
        loginProgress = binding.loginProgress
        binding.loginBtn.setOnClickListener {
            attemptLogin(binding.userNameEdit.text.toString(), binding.passWordEdit.text.toString())
        }
        binding.registerBtn.setOnClickListener {
            //跳转到注册界面
            replaceFragment(RegisterFragment(), true)
        }
        binding.showPasswordCb.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                binding.passWordEdit.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            } else {
                binding.passWordEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            }
        }
    }

    private fun attemptLogin(name: String, pwd: String) {
        if (isUserNameValid(name) && isPasswordValid(pwd)) {
            loginProgress.visibility = View.VISIBLE
            AVUser.logInInBackground(name, pwd, object : LogInCallback<AVUser>() {
                override fun done(p0: AVUser?, p1: AVException?) {
                    loginProgress.visibility = View.GONE
                    if (p1 == null) {//登录成功
                        isLogin = true
                        replaceFragment(MainFragment.newInstance(), false)
                    } else {
                        Toast.makeText(context, p1.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        } else {
            loginProgress.visibility = View.GONE

        }
    }

    private fun isUserNameValid(userName: String): Boolean {
        if (userName.isNullOrBlank()) {
            Toast.makeText(context, "请输入用户名", Toast.LENGTH_SHORT).show()
            return false
        } else {
            return true
        }
    }

    private fun isPasswordValid(password: String): Boolean {
        if (password.isNullOrBlank()) {
            Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show()
            return false
        } else if (password.length < 4) {
            Toast.makeText(context, "密码长度需大于3位", Toast.LENGTH_SHORT).show()
            return false
        } else {
            return true
        }
    }
}